import { createRouter, createWebHashHistory } from "vue-router";
import HomeView from "../views/home/index.vue";
import { useUserStore } from "@/stores/user";

const routes = [
    {
        path: "/",
        name: "home",
        component: HomeView,
    },
    {
        path: "/kind",
        name: "kind",
        component: () => import("../views/kind/index.vue"),
    },
    {
        path: "/cart",
        name: "cart",
        component: () => import("../views/cart/index.vue"),
    },
    {
        path: "/my",
        name: "my",
        component: () => import("../views/my/index.vue"),
    },
    {
        path: "/login",
        name: "login",
        // 利用 路由元信息 帮助我们区分到底那个页面需要展示 底部导航
        meta: {
            // 当前的对象内部可以书写我们的路由元信息
            hidden: true, // 路由元信息说白了就是给路由添加一些特殊的标识
        },
        component: () => import("../views/login/index.vue"),
    },
    {
        path: "/register",
        name: "register",
        meta: {
            hidden: true,
        },
        component: () => import("../views/register/index.vue"),
        children: [
            {
                path: "no1",
                name: "no1",
                component: () => import("../views/register/no1.vue"),
            },
            {
                path: "no2",
                name: "no2",
                component: () => import("../views/register/no2.vue"),
            },
            {
                path: "no3",
                name: "no3",
                component: () => import("../views/register/no3.vue"),
            },
        ],
    },
    {
        path: "/detail",
        name: "detail",
        meta: {
            hidden: true,
        },
        component: () => import("../views/detail/index.vue"),
    },
];

const router = createRouter({
    history: createWebHashHistory(import.meta.env.BASE_URL),
    routes,
});

router.beforeEach((to, from) => {
    // console.log(to);
    const userStore = useUserStore();

    if (to.fullPath === "/cart" || to.fullPath === "/my") {
        // console.log('进入购物车页面和个人页面应该先验证登陆状态', userStore.loginState)

        if (!userStore.loginState) {
            router.push("/login");
        }
    }
});

export default router;
